package com.tanhua.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.*;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.DELETE;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private BlackListService blackListService;

    /**
     * 查询用户资料
     *
     * @param userID 该参数可有可无，如果值为空就查询当前用户的全部数据
     * @return
     */
    @GetMapping
    public ResponseEntity queryById(Long userID) {
        //判断传递的useID是否为空
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfoVo = userInfoService.queryById(userID);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户信息
     *
     * @param userInfo 该属性并未封装ID的值，可以从请求头携带的token中获取
     * @return
     */
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.updateById(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 更新用户头像
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity uploadImage(@RequestBody MultipartFile headPhoto) throws IOException {
        userInfoService.update(headPhoto, UserHolder.getUserId());
        return ResponseEntity.ok(null);

    }

    /**
     * 查询通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity queryCommonSetting(){
        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置陌生人问题（保存）
     */
    @PostMapping("/questions")
    public ResponseEntity setQuestion(@RequestBody Map map){
        String content = (String) map.get("content");
        questionService.setQuestion(content);
        return ResponseEntity.ok(null);
    }
    /**
     * 设置通知开关
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity setSetttings(@RequestBody Settings settings){
        settingsService.setSettings(settings);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询黑名单，分页
     */
    @GetMapping("/blacklist")
    public ResponseEntity queryBlackList(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pagesize){
        //调用service查询
        PageResult pr = settingsService.blackList(page,pagesize);
        //构造返回
        return ResponseEntity.ok(pr);
    }

    /**
     * 黑名单移除
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteByUid(@PathVariable("uid") Long uid){
        blackListService.delete(uid);
        return ResponseEntity.ok(null);
    }
    /**
     * 修改手机号时发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendMsg(){
        userService.sendMsg(UserHolder.getUserMobile());
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkCode(@RequestBody Map map){
        String code = (String) map.get("verificationCode");
        System.out.println(code);
        Map checkMap = userService.checkCode(UserHolder.getUserMobile(), code);
        return ResponseEntity.ok(checkMap);
    }

    /**
     * 保存新的手机号
     */
    @PostMapping("/phone")
    public ResponseEntity saveNewPhone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userService.updatePhone(phone);
        return ResponseEntity.ok(null);

    }

    /**
     * 互相喜欢，喜欢，粉丝的数量
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map map = userService.counts();
        return ResponseEntity.ok(map);
    }
    /**
     * 查询"谁看过我"访客列表
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity lookedMeList(@PathVariable("type") String type ,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pagesize,String nickname){
        PageResult pr = userService.list(type,page,pagesize);
        return ResponseEntity.ok(pr);
    }

}
